package com.car.web.controller.index;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.car.dal.dao.client.TgClientDao;
import com.car.dal.dao.office.TgOfficeDao;
import com.car.dal.dao.rent.RentDao;
import com.car.dal.dao.rent.RentDetailDao;
import com.car.dal.dao.rent.RentHouseInformationDao;
import com.car.dal.dao.reserve.TgReserveDao;
import com.car.dal.dao.user.UserinfoDao;
import com.car.dal.dao.usergroup.TgUsergroupDao;
import com.car.dal.object.Cout;
import com.car.dal.object.SyqClient;
import com.car.dal.object.TgClient;
import com.car.dal.object.TgOffice;
import com.car.dal.object.TgReserve;
import com.car.dal.object.TgUser;
import com.car.dal.object.TgUserinfo;
import com.car.dal.object.rent.Rent;
import com.car.dal.object.rent.RentDetail;
import com.car.dal.object.rent.RentHouseInformation;
import com.car.util.DateUtil;
import com.car.util.JsonExecutor;
import com.car.util.Page;
import com.car.util.PageUtil;
import com.car.util.Result;
import com.car.util.ResultSupport;
import com.car.util.UserUtil;
import com.car.util.WebExecutor;
import com.car.util.JsonExecutor.JsonBiz;
import com.car.util.WebExecutor.Biz;
import com.car.web.controller.base.BaseVo;
/**
 * 房租
 * @author d
 *
 */
@Controller
@RequestMapping("/rent")
public class RentControler {
	private static final Logger logger = Logger
			.getLogger(ConsuControllerZh.class);
	public static 	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	@Resource 
	private RentDao rentDao;
	@Resource
	private TgOfficeDao tgOfficeDao;
	@Resource
	private RentHouseInformationDao rentHouseInformationDao;
	@Resource
	private RentDetailDao rentDetailDao;
	@Resource
	private UserinfoDao userinfoDao;
	@Resource
	private TgClientDao clientDao;
	@Resource
    private TgReserveDao reserveDao;
	/**
	 * 展现房租的缴费信息
	 * @param modelMap
	 * @param vo
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/rentInforList")
	public ModelAndView rentInforList(ModelMap modelMap,
			final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo , new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try { 
					RentHouseInformation rentHouseInformation=new RentHouseInformation();
			    	String center=request.getParameter("center");
				    String start_date=request.getParameter("start_date");
				    if(center!=null&&!"".equals(center)){
					    rentHouseInformation.setCenter(center);
				    }
			       if(start_date!=null&&!"".equals(start_date)){ 
			    	   start_date=start_date+"-1";
			    	   Date w=DateUtil.getEndMonth(start_date);
			    	   String last=sdf.format(w);
			    	   Date last_date=sdf.parse(last);
			    	   Date d = sdf.parse(start_date);
			    	   rentHouseInformation.setLast_date(last_date.getTime()/1000);
			    	   rentHouseInformation.setStart_date(d.getTime()/1000);
			       }
				   Integer total=rentHouseInformationDao.getCountRentInforByType(rentHouseInformation);
				   Integer cur=vo.getCurrPage()==null?1:vo.getCurrPage();
				   Page page=PageUtil.createPage(8, cur, total);
				   List<RentHouseInformation>list=rentHouseInformationDao.getRentInforByType(rentHouseInformation, page);
			       List<Rent>lt=new ArrayList<Rent>();
			       if(list!=null&&list.size()>0){
				    	for(int i=0;i<list.size();i++){
						   Rent rent=new Rent();
						   rent.setRent_Infor_id(list.get(i).getId());
						   List<Rent>ls=rentDao.getRentByType1(rent);
						   if(ls!=null&&ls.size()>0){
							  if(ls.get(0).getMaturitydate()!=null){
								  String w=compareTime(ls.get(0).getMaturitydate());
								  ls.get(0).setLeaveMessage(w);
							   }
							   if(ls.get(0).getDuedate()!=null){
								  String d=compareTime(ls.get(0).getDuedate());
								  ls.get(0).setLeaveMessage1(d);
							   }	
							   lt.add(ls.get(0));
						   }else{
							   Rent rt=new Rent();
							   rt.setRent_Infor_id(list.get(i).getId());
							   rt.setAddress(list.get(i).getAddress());
							   rt.setCenter(list.get(i).getCenter());
							   rt.setOwnersituation(list.get(i).getOwnersituation());
							   rt.setContactperson(list.get(i).getContactperson());
							   rt.setAgencybail(list.get(i).getAgencybail());
							   rt.setDepositsituation(list.get(i).getDepositsituation());
							   rt.setDepositmoney(list.get(i).getDepositmoney());
							   rt.setHt_url(list.get(i).getHt_url());
							   lt.add(rt);
						  }
					    }
				    }
				    List<TgOffice> office_List = tgOfficeDao.findAll(TgOffice.class);//查询中心
				    result.setDefaultModel("office_List", office_List);
				    result.setDefaultModel("list", lt);
				    result.setDefaultModel("page", page);
				} 	catch (ParseException e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("work/rent/rent_infor_list", modelMap);
	}
	
	@RequestMapping(value = "/add_houseinformation")
	public ModelAndView add_houseInformation(ModelMap modelMap,
			final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo , new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String id=request.getParameter("id");
				Rent rent=new Rent();
				if(id!=null&&!"".equals(id)){
					rent.setRent_Infor_id(Long.parseLong(id));
					List<Rent>list=rentDao.getRentByType1(rent);
					if(list!=null&&list.size()>0){
						result.setDefaultModel("rent", list.get(0));
					}
				}
				
				List<TgOffice> office_List = tgOfficeDao.findAll(TgOffice.class);//查询中心
				result.setDefaultModel("office_List", office_List);
				return result;
			}
		});
		return new ModelAndView("work/rent/add_rent_infor", modelMap);
	}
	/**
	 * 添加房屋信息
	 * @param modelMap
	 * @param vo
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addhouseinformation")
	public ModelAndView addHouseInformation(ModelMap modelMap,
			final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap =  JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				RentHouseInformation rentHI=new RentHouseInformation();
				String office_id=request.getParameter("office_id");
				if(office_id!=null&&!"".equals(office_id)){
					 TgOffice office=tgOfficeDao.findById(TgOffice.class, Long.parseLong(office_id));
					 rentHI.setAddress(office.getAddress());
					 rentHI.setCenter(office.getName());
				}
				String id=request.getParameter("id");
				String fixpaymenttime=request.getParameter("fixpaymenttime");
				String ht_url=request.getParameter("hturl");
				if(ht_url!=null&&!"".equals(ht_url)&&!ht_url.equals(",")){
					String []p=ht_url.split(",");
					if(p.length>1){
						rentHI.setHt_url(p[1]);
					}else{
						rentHI.setHt_url(ht_url);
					}
				}
				String ownersituation=request.getParameter("ownersituation");
				String contactperson=request.getParameter("contactperson");
				String agencybail=request.getParameter("agencybail");
				String depositsituation=request.getParameter("depositsituation");
				String depositmoney=request.getParameter("depositmoney");
				rentHI.setFixpaymenttime(fixpaymenttime);
				rentHI.setOwnersituation(ownersituation);
				rentHI.setContactperson(contactperson);
				rentHI.setAgencybail(agencybail);
				rentHI.setDepositsituation(depositsituation);
				rentHI.setDepositmoney(depositmoney);
				if(id!=null&&!"".equals(id)){
					rentHI.setId(Long.parseLong(id));
					rentHouseInformationDao.update(rentHI);
				}else{
					rentHouseInformationDao.insert(rentHI);
				}
				result.setDefaultModel("msg", "1");
				return result;
			}
				
		});
		return new ModelAndView("work/rent/rent_infor_list", modelMap);
	}
	@RequestMapping(value = "/rentList")
	public ModelAndView rentList(ModelMap modelMap,
			final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo , new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				Rent rent=new Rent();
				Integer cur=vo.getCurrPage()==null?1:vo.getCurrPage();
				String rent_infor=request.getParameter("rent_infor_id");
				if(rent_infor!=null&&!"".equals(rent_infor)){
					rent.setRent_Infor_id(Long.parseLong(rent_infor));//查看的时候
				}
				Integer total=rentDao.getCountRentByType(rent);
				Page page=PageUtil.createPage(8, cur, total);
				List<Rent> list=rentDao.getRentByType(rent, page);
			    if(list!=null&&list.size()>0){
			    	for(int i=0;i<list.size();i++){
			    	    String x=compareTime(list.get(i).getMaturitydate());//到期时间与现在比较
			    	    if(x!=null&&!"".equals(x)){
			    		   list.get(i).setLeaveMessage(x);//距离到期还有六个月的筛选出来，否则删除掉
			    	    }
			       }
			    }
				result.setDefaultModel("list", list);
				result.setDefaultModel("Page", page);
				return result;
			}
		});
		return new ModelAndView("work/rent/rent_list", modelMap);
	}
	
	@RequestMapping(value = "/add_rent")
	public ModelAndView add_rent(ModelMap modelMap,
			final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap =  JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String rent_Infor_id=request.getParameter("rent_Infor_id");
				String id=request.getParameter("id");
				Rent rent=new Rent();	
				if(rent_Infor_id!=null&&!"".equals(rent_Infor_id)){//在添加的时候
					rent.setRent_Infor_id(Long.parseLong(rent_Infor_id));
					List<Rent>list=rentDao.getRentByType1(rent);
					if(list!=null&&list.size()>0){
						
						result.setDefaultModel("rent", list.get(0));
					}else{
						result.setDefaultModel("rent", rent);
					}
					result.setDefaultModel("type", "1");
				}
				if(id!=null&&!"".equals(id)){//在修改的时候
					rent.setId(Long.parseLong(id));
					Rent rs=rentDao.findById(Rent.class, Long.parseLong(id));
					result.setDefaultModel("rent", rs);
				}
				return result;
			}
				
		});
		return new ModelAndView("work/rent/add_rent", modelMap);
	}
	
	/**
	 * 添加房屋租金
	 * @param modelMap
	 * @param vo
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/rent_add")
	public ModelAndView rent_add(ModelMap modelMap,
			final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap =  JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try {
					TgUser user = UserUtil.getUserFromCookies();
					RentHouseInformation rentHouseInformation=new RentHouseInformation();
					String duedate=request.getParameter("duedate");
					String rent_Infor_id=request.getParameter("rent_Infor_id");
					String id=request.getParameter("id");
					String handler=request.getParameter("handler");
					String dateofdeposit=request.getParameter("dateofdeposit");
					String maturitydate=request.getParameter("maturitydate");
					String ren=request.getParameter("rent");
					String accountHolder=request.getParameter("accountHolder");
					String accountNumber=request.getParameter("accountNumber");
					String bank=request.getParameter("bank");
					String payment=request.getParameter("payment");
					String type=request.getParameter("type");
					String paymentcycle=request.getParameter("paymentcycle");
					Rent rent=new Rent();
					if(rent_Infor_id!=null&&!"".equals(rent_Infor_id)){
						rent.setRent_Infor_id(Long.parseLong(rent_Infor_id));
						rentHouseInformation.setId(Long.parseLong(rent_Infor_id));
					}
					rent.setHandler(handler);
					rent.setDateofdeposit(dateofdeposit);
					Date m = sdf.parse(maturitydate);
					rent.setMaturitydate(m);
					Date d=sdf.parse(duedate);
					rentHouseInformation.setUdate(d.getTime()/1000);
					rent.setDuedate(d);
					rent.setRent(ren);
					rent.setAccountHolder(accountHolder);
					rent.setAccountNumber(accountNumber);
					rent.setBank(bank);
					rent.setPayment(payment);
					rent.setPaymentcycle(paymentcycle);
					rent.setCreate_time(new Date());
					rent.setCreateName(user.getUsername());
					if(type!=null&&!"".equals(type)){//说明是添加
						rentDao.insert(rent);
					}else if(id!=null&&!"".equals(id)){
						rent.setId(Long.parseLong(id));
						rentDao.update(rent);
					}else{
						rentDao.insert(rent);
					}
					if(rentHouseInformation.getId()!=null){
						rentHouseInformationDao.update(rentHouseInformation);
					}
					result.setDefaultModel("msg", "1");
				}catch (ParseException e) {
					
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("work/rent/rent_infor_list", modelMap);
	}
	@RequestMapping(value = "/rentListDetail")
	public ModelAndView rentListDetail(ModelMap modelMap,
			final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo , new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				Integer cur=vo.getCurrPage()==null?1:vo.getCurrPage();
				String tg_rent=request.getParameter("tg_rent");
				RentDetail rentDetail=new RentDetail();
				if(tg_rent!=null&&!"".equals(tg_rent)){
					 rentDetail.setTg_rent(Long.parseLong(tg_rent));//查看的时候
					 Integer total=rentDetailDao.getCountRentDetailByType(rentDetail);
					 Page page=PageUtil.createPage(8, cur, total);
					 List<RentDetail>list=rentDetailDao.getRentDetailByType(rentDetail, page);
					 result.setDefaultModel("list", list);
					 result.setDefaultModel("Page", page);
				}else{
					 Page page=PageUtil.createPage(8, 0, 0);
					 result.setDefaultModel("Page", page);
				}
				return result;
			}
		});
		return new ModelAndView("work/rent/rent_detail_list", modelMap);
	}
	
	@RequestMapping(value = "/rent_add_detail")
	public ModelAndView rent_add_detail(ModelMap modelMap,
			final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap =  WebExecutor.execute(logger, vo , new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String tg_rent=request.getParameter("tg_rent");
				RentDetail rentDetail=new RentDetail();
				if(tg_rent!=null&&!"".equals(tg_rent)){
					rentDetail.setTg_rent(Long.parseLong(tg_rent));
					List<RentDetail>list=rentDetailDao.getRentDetailByType1(rentDetail);
					if(list!=null&&list.size()>0){
						result.setDefaultModel("rentDetail", list.get(0));
					}else{
						rentDetail.setTg_rent(Long.parseLong(tg_rent));
						result.setDefaultModel("rentDetail", rentDetail);
					}
				}
				return result;
			}
		});
		return new ModelAndView("work/rent/add_rent_detail", modelMap);
	}
	@RequestMapping(value = "/rentAddDetail")
	public ModelAndView rentAddDetail(ModelMap modelMap,
			final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap =  JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				try {
					
					RentDetail rentDetail=new RentDetail();
					String tg_rent=request.getParameter("tg_rent");
					String paymentcycle=request.getParameter("paymentcycle");
					String monthmoney=request.getParameter("monthmoney");
					String yearmoney=request.getParameter("yearmoney");
					String yearsofsignature=request.getParameter("yearsofsignature");
					String contracttime=request.getParameter("contracttime");
					String maturityfollowup=request.getParameter("maturityfollowup");
					String propertyfeeholder=request.getParameter("propertyfeeholder");
					String propertycharges=request.getParameter("propertycharges");
					String otherexpenses=request.getParameter("otherexpenses");
					String otherconventions=request.getParameter("otherconventions");
					if(tg_rent!=null&&!"".equals(tg_rent)){
						rentDetail.setTg_rent(Long.parseLong(tg_rent));
					}
					rentDetail.setPaymentcycle(paymentcycle);
					rentDetail.setMonthmoney(monthmoney);
					rentDetail.setYearmoney(yearmoney);
					rentDetail.setYearsofsignature(yearsofsignature);
					Date d=sdf.parse(contracttime);
					rentDetail.setContracttime(d);
					rentDetail.setMaturityfollowup(maturityfollowup);
					rentDetail.setPropertycharges(propertycharges);
					rentDetail.setPropertyfeeholder(propertyfeeholder);
					rentDetail.setOtherexpenses(otherexpenses);
					rentDetail.setOtherconventions(otherconventions);
					rentDetailDao.insert(rentDetail);
					result.setDefaultModel("msg", "1");
				}catch (ParseException e) {
					
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("consultant/consultant_import_add", modelMap);
	}
    public String compareTime(Date date){
    	String d1=sdf.format(date);
    	String d2=sdf.format(new Date());
    	String ds1[]=d1.split("-");
    	String ds2[]=d2.split("-");
    	String x="";
    	if(Integer.parseInt(ds1[0])>Integer.parseInt(ds2[0])){
    		if(Integer.parseInt(ds1[0])-Integer.parseInt(ds2[0])>1){//大于一说明至少有一年，不需要半年提醒
    			return x;
    		}else if(Integer.parseInt(ds1[1])-Integer.parseInt(ds2[1])>=-5){
    			return x;//说明房租大于半年
    		}else if(Integer.parseInt(ds1[1])-Integer.parseInt(ds2[1])==-6){
    	        if(Integer.parseInt(ds1[2])-Integer.parseInt(ds2[2])>=0){
    	        	return x;
    	        }else{
    	        	int c=Integer.parseInt(ds2[2])-Integer.parseInt(ds1[2]);
    	        	x="5个月"+c+"天";
    	        	return x;
    	        }
    		}else{
    			if(Integer.parseInt(ds1[2])-Integer.parseInt(ds2[2])>=0){
    				int c=Integer.parseInt(ds1[2])-Integer.parseInt(ds2[2]);
    				int b=12-Integer.parseInt(ds2[1])+Integer.parseInt(ds1[1]);
    				x=b+"个月"+c+"天";
    			}else{
    				int c=30+Integer.parseInt(ds1[2])-Integer.parseInt(ds2[2]);
    				int b=11-Integer.parseInt(ds2[1])+Integer.parseInt(ds1[1]);
    				x=b+"个月"+c+"天";
    			}
    			return x;
    		}
    	}else if(Integer.parseInt(ds1[0])-Integer.parseInt(ds2[0])==0){
    		if(Integer.parseInt(ds1[1])-Integer.parseInt(ds2[1])>6){
    			return x;
    		}else{
    			if(Integer.parseInt(ds1[2])-Integer.parseInt(ds2[2])>0){
    				int c=Integer.parseInt(ds1[2])-Integer.parseInt(ds2[2]);
    				int b=Integer.parseInt(ds1[1])-Integer.parseInt(ds2[1]);
    				x=b+"个月"+c+"天";
    			}else{
    				int c=30+Integer.parseInt(ds1[2])-Integer.parseInt(ds2[2]);
    				int b=Integer.parseInt(ds1[1])-Integer.parseInt(ds1[1])-1;
    				x=b+"个月"+c+"天";
    			}
    			return x;
    		}
    	}else{
    		return "已经过期";//这种情况应该不会出现，已经欠租了
    	}
    }
    
    @RequestMapping(value = "/zxsTongj")//中心到访数量统计
	public ModelAndView zxsTongj(ModelMap modelMap,
			final BaseVo vo, final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		modelMap = WebExecutor.execute(logger, vo , new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				    String date=request.getParameter("date");
					String name=request.getParameter("name");
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					DecimalFormat fnum = new DecimalFormat("##0.00%");
					
					String today = "";
					String BeforetwoDay = "";
					String BeforeDay = "";
					String tommorrow1 = "";
					String tommorrow2 = "";
					String tommorrow3 = "";
					String tommorrow4 = "";
					String newday = "";
					String first = "";
					String end = "";
					Date currentDate = null;
					if (date != null && !"".equals(date)) {
						try {
							currentDate = sdf.parse(date);
						} catch (ParseException e) {
							e.printStackTrace();
						} // 今天
						newday = date;
						Calendar rightNow = Calendar.getInstance();
						rightNow.setTime(currentDate);
						rightNow.add(Calendar.DAY_OF_YEAR, -1);
						String ate = sdf.format(rightNow.getTime());
						try {
							currentDate = sdf.parse(ate);
						} catch (ParseException e) {
							e.printStackTrace();
						}
					} else {
						newday = sdf.format(DateUtil.getToDay());
						currentDate = new Date();
						Calendar rightNow = Calendar.getInstance();
						rightNow.setTime(currentDate);
						rightNow.add(Calendar.DAY_OF_YEAR, -1);
						String ate = sdf.format(rightNow.getTime());
						try {
							currentDate = sdf.parse(ate);
						} catch (ParseException e) {
							e.printStackTrace();
						}
					}
					List<Date> days = DateUtil.dateToWeek(currentDate);
					first = sdf.format(DateUtil.DateAdd(days.get(0), -1));
					end = sdf.format(DateUtil.DateAdd(days.get(6), 1));
					days.add(7, DateUtil.DateAdd(days.get(6), 1));

					BeforetwoDay = sdf.format(days.get(0)).substring(0, 10);
					BeforeDay = sdf.format(days.get(1)).substring(0, 10);
					today = sdf.format(days.get(2)).substring(0, 10);
					tommorrow1 = sdf.format(days.get(3)).substring(0, 10);
					tommorrow2 = sdf.format(days.get(4)).substring(0, 10);
					tommorrow3 = sdf.format(days.get(5)).substring(0, 10);
					tommorrow4 = sdf.format(days.get(6)).substring(0, 10);
					
					
					
					List<SyqClient> syqlist = new ArrayList<SyqClient>();
					TgUserinfo tg = new TgUserinfo();
					if (name != null && !"".equals(name)) {
						tg.setNickname(name);
					}
					List<TgUserinfo> tglist = userinfoDao.getzxNotZhuanz(tg);
					if(tglist!=null&&tglist.size()>0){
						for(TgUserinfo tgu : tglist){
							Integer zoncount=0;
							Integer wuxiaocount=0;
							Integer youxiaocount=0;
							Integer bendicount=0;
							Integer waidicount=0;
							Integer yuyuecount=0;
							float   yuyuebili=0;
							String rate;
							
							TgClient client = new TgClient();
							TgReserve reserve = new TgReserve();
							SyqClient syq = new SyqClient();
							client.setStartTime(days.get(0).getTime() / 1000);
							client.setEndTime(days.get(6).getTime() / 1000);
							client.setUsername(tgu.getUsername());
							reserve.setSnickname(tgu.getNickname());
							
							reserve.setSdate2(days.get(0).getTime() / 1000);
							
							reserve.setEdate2(days.get(6).getTime() / 1000);
							reserve.setNon(0);
							
							zoncount = clientDao.getZonClientCount(client);// 客户总量
							wuxiaocount = clientDao.getWuxiaoClientCount(client);// 无效客户数量
							youxiaocount = (zoncount - wuxiaocount);// 有效客户总数量
							bendicount = clientDao.getBendiClientCount(client);// 本地客户数量
							waidicount = clientDao.getWaidiClientCount(client);// 外地客户数量
							yuyuecount = reserveDao.getReserveCount(reserve);// 预约测评数量
							System.out.println(youxiaocount);
							System.out.println(yuyuecount);
							if(youxiaocount>0){
								yuyuebili = (float) yuyuecount / bendicount;
								rate = fnum.format(yuyuebili);
							}else{
								rate = fnum.format(0);
							}
							
							syq.setCenter(tgu.getOff_name());
							syq.setNickname(tgu.getNickname());
							syq.setZonnumber(youxiaocount);
							syq.setWdzy(waidicount);
							syq.setZxzy(bendicount);
							syq.setYuybili(rate);
							syq.setYuynum(yuyuecount);
							
							
							
							syqlist.add(syq);
						}
					}
					result.setDefaultModel("syqlist", syqlist);
					result.setDefaultModel("BeforetwoDay", BeforetwoDay);
					result.setDefaultModel("BeforeDay", BeforeDay);
					result.setDefaultModel("today", today);
					result.setDefaultModel("tommorrow1", tommorrow1);
					result.setDefaultModel("tommorrow2", tommorrow2);
					result.setDefaultModel("tommorrow3", tommorrow3);
					result.setDefaultModel("tommorrow4", tommorrow4);
					result.setDefaultModel("newday", newday);
					
				return result;
			}
		});
		return new ModelAndView("consultant/consultant_week_wzz", modelMap);
	}
    
    
    //邀约到访统计
    @RequestMapping(value = "/yuyue_week_stat")
    public ModelAndView  yuyue_week_stat(ModelMap modelMap, final BaseVo vo,
                                            final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    String time = request.getParameter("start_date");
                    
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    DecimalFormat fnum = new DecimalFormat("##0.00%");
                    
                    String MonDayweek = "";//周一
                    String Sundayweek = "";//周日
                    
                    String MonDaybefore = "";//上周一
                    String Sundaybefore = "";//上周日
                    
                    String newday = "";
                    String beforeday = "";
                    Date currentDate = null;
                    Date currentDate2 = null;
                    if (time != null && !"".equals(time)) {
                        currentDate = sdf.parse(time);// 今天
                        newday = time;
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(currentDate);
                        String ate = sdf.format(rightNow.getTime());
                        currentDate = sdf.parse(ate);
                        
                        currentDate2 = sdf.parse(time);
                        Calendar rightNow2 = Calendar.getInstance();
                        rightNow2.setTime(currentDate2);
                        rightNow2.add(Calendar.DAY_OF_YEAR, -7);
                        String ate2 = sdf.format(rightNow2.getTime());
                        currentDate2 = sdf.parse(ate2);
                        
                    } else {
                        newday = sdf.format(DateUtil.getToDay());
                        currentDate = new Date();
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(currentDate);
                        String ate = sdf.format(rightNow.getTime());
                        currentDate = sdf.parse(ate);
                        
                        currentDate2 = new Date();
                        Calendar rightNow2 = Calendar.getInstance();
                        rightNow2.setTime(currentDate2);
                        rightNow2.add(Calendar.DAY_OF_YEAR, -7);
                        String ate2 = sdf.format(rightNow2.getTime());
                        currentDate2 = sdf.parse(ate2);
                    }
                    List<Date> days = DateUtil.dateToWeek(currentDate);
                    MonDayweek = sdf.format(days.get(0)).substring(0, 10);//本周一
                    Sundayweek = sdf.format(days.get(6)).substring(0, 10);//本周日
                    List<Date> days2 = DateUtil.dateToWeek(currentDate2);
                    MonDaybefore = sdf.format(days2.get(0)).substring(0, 10);//上周一
                    Sundaybefore = sdf.format(days2.get(6)).substring(0, 10);//上周日
                    int total = 0;  int not = 0; int real = 0;  
                    int total2 = 0; int not2 = 0; int real2 = 0;
                    List<TgOffice> office_List = tgOfficeDao.findAll(TgOffice.class);
                    List<TgOffice> list = new ArrayList<TgOffice>();
                    TgOffice office = new TgOffice();
                    TgOffice office2 = new TgOffice();
                    
                    if (office_List != null && office_List.size() > 0) {
                        for (TgOffice off : office_List) {
                            if (!off.getKeyname().equals("CCEE06DEB51D")) {
                            	//本周
                                office.setStaTime(sdf.parse(MonDayweek).getTime() / 1000);
                                office.setEnTime(sdf.parse(Sundayweek).getTime() / 1000);
                                office.setRegion(off.getKeyname());
                                int total_reserve = tgOfficeDao.getCenterTotal(office);//总计
                                int not_visit = tgOfficeDao.getCenterNot(office);//未到
                                int real_visit = tgOfficeDao.getCenterReal(office);//实到
                                if(total_reserve>=8){
                                	off.setTotal_reserve(total_reserve);
                                    off.setNot_visit(not_visit);
                                    off.setReal_visit(real_visit);
                                  //总计
                                    total += total_reserve;
                                    not += not_visit;
                                    real += real_visit;
                                    //上周
                                    office2.setStaTime(sdf.parse(MonDaybefore).getTime() / 1000);
                                    office2.setEnTime(sdf.parse(Sundaybefore).getTime() / 1000);
                                    office2.setRegion(off.getKeyname());
                                    int total_weekold = tgOfficeDao.getCenterTotal(office2);//总计
                                    int not_weekold = tgOfficeDao.getCenterNot(office2);//未到
                                    int  real_weekold= tgOfficeDao.getCenterReal(office2);//实到
                                    
                                    off.setTotal_weekold(total_weekold);
                                    off.setReal_weekold(real_weekold);
                                    off.setNot_weekold(not_weekold);
                                    //总计
                                    total2 += total_weekold;
                                    not2 += not_weekold;
                                    real2 += real_weekold;
                                    
                                    list.add(off);
                                }
                            }
                        }

                    }
                    result.setDefaultModel("date3", sdf.format(new Date()));
                    result.setDefaultModel("list", list);
                    result.setDefaultModel("Sundayweek", Sundayweek);
                    result.setDefaultModel("MonDayweek", MonDayweek);
                    result.setDefaultModel("MonDaybefore", MonDaybefore);
                    result.setDefaultModel("Sundaybefore", Sundaybefore);
                    result.setDefaultModel("newday", newday);
                    result.setDefaultModel("office", office);
                    result.setDefaultModel("total", total);
                    result.setDefaultModel("not", not);
                    result.setDefaultModel("real", real);
                    result.setDefaultModel("total2", total2);
                    result.setDefaultModel("not2", not2);
                    result.setDefaultModel("real2", real2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });
        return new ModelAndView("consultant/yaoylv/yuyue_week_stat", modelMap);
    }
}
